<template>
  <div class="data-table-wrap">
    <el-table
        @expand-change="expandChange"
        v-loading="tableLoading"
        element-loading-background="rgba(123,214,220,0.2)"
        element-loading-text="加载中"
        :border="border"
        :data="tableData"
        :span-method="tableCellMerge"
        :max-height="maxHeight"
        :header-cell-style="headerCellStyle"
        @selection-change="handleSelectionChange"
        style="width: 100%;height: calc(100% - 40px); text-align: center;z-index: 1"
        :style="{height:(isHasPagination?'calc(100% - 40px)':'100%')}">
      <el-table-column
        type="selection"
        v-if="selection"
        width="40">
      </el-table-column>
      <el-table-column
          v-if="hasIndex"
          type="index"
          label="序号"
          fixed
          align="center"
          width="60">
      </el-table-column>
      <slot name="pre"></slot>
      <template v-for="item in tableHead">
        <el-table-column
            v-if="item.image"
            :formatter="item.formatter"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :fixed="item.fixed"
            :sortable="item.sortable"
            align="center">
          <template slot-scope="scope">
            <img :style="{width:item.imageWidth,height:item.imageHeight}"
                 class="cell-img"
                 :src="item.baseImgUrl+scope.row[item.prop]" alt="">
          </template>
        </el-table-column>
        <el-table-column
            v-else
            :formatter="item.formatter"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :fixed="item.fixed"
            :min-width="item.minWidth"
            :sortable="item.sortable"
            align="center">
        </el-table-column>
      </template>
      <slot></slot>
    </el-table>
    <!--        class="pagination-main"-->
    <div v-if="isHasPagination">
      <slot name="pagination-pre"></slot>
      <el-config-provider :locale="zhCn">
        <el-pagination
            class="pagination-class"
            :total="total"
            small
            :page-size="pageSize"
            :current-page="currentPage"
            @current-change="handleCurrentChange"
            background
            :layout="layout"
            style="text-align: center;">
        </el-pagination>
      </el-config-provider>
      <span class="page-size" v-if="hasPageSize">
                  <span class="span-class">每页</span>
                  <el-input class="input-wrap" v-model="changePageSize"></el-input>
                  <i class="el-icon-refresh" style="margin-left:20px;cursor:pointer"
                     @click="handleChangePageSize"></i>
                  <span class="span-class">条</span>
              </span>
      <slot name="pagination-back"></slot>
    </div>
  </div>
</template>

<script>
import {defineComponent, reactive, onMounted, toRefs, ref, watchEffect} from 'vue'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'

export default defineComponent(
    {
      name: 'dataTable',
      props: {
        selection: false,                  // 是否需要复选框
        tableLoading: null,
        tableHead: {
          require: true,
          default: []
        },
        tableData: {
          require: true,
          default: []
        },
        tableHeight: {
          type: String,
          default: '100%'
        },
        maxHeight: null,
        tableCellMerge: {
          type: Function
        },
        tableCellFormatter: {
          type: Function
        },
        total: null,
        pageSize: null,
        isHasPagination: {
          type: Boolean,
          default: true
        },
        hasIndex: false,
        border: false,
        hasPageSize: false,      // 是否有页码设置框
        headerCellStyle: {
          default: () => {
            return {
              // backgroundImage: 'linear-gradient(to bottom, #02bbdfdd, #32b3f5dd)',
              background: 'rgba(1,90,90,0.8)',
              color: '#3aecff',
              textShadow: '0px 0px 1px #3e5392'
            }
          }
        },
        layout: {
          default: 'total, prev, pager, next, jumper',
          require: false
        },
        currentPage: {
          type: Number,
          default: 1
        }
      },
      setup(props, {emit}) {
        let changePageSize = ref(0);
        onMounted(() => {
          changePageSize = props.pageSize
        })
        const headRowStyle = ({row, rowIndex}) => {

        }
        const bodyHeight = () => {
          return `calc(${props.tableHeight} - 37px)`
        }
        const expandChange = (row, expandedRows) => {
          emit('expandChange', row, expandedRows)
        }
        const handleCurrentChange = (val) => {
          emit("changeCurrentPage", val)
        }
        const handleChangePageSize = () => {
          emit("changePageSize", changePageSize.value)
        }

        const handleSelectionChange=(val)=>{
          emit("selection-change", val)
        }
        return {
          changePageSize,
          zhCn,
          headRowStyle,
          bodyHeight,
          expandChange,
          handleCurrentChange,
          handleChangePageSize,
          handleSelectionChange
        }
      }
    })
</script>

<style>
.el-table td, .el-table th.is-leaf {
  border-bottom: 1px solid #f3f6fd !important;
}

.data-table-wrap .el-table th.is-leaf {
  background-image: linear-gradient(rgba(2, 187, 223, 0.867), rgba(50, 179, 245, 0.867));
}
</style>

<style scoped lang="scss">
.data-table-main {
  height: calc(100% - 150px);
  //height: 700px;
  margin-left: 20px;
  margin-right: 20px;
}

.data-table-wrap {
  //border-radius: 10px;
  //box-shadow: 0px 2px 5px 1px #e5e5e7;
  //border: 1px solid #fff0f0;
  //background-color: #fff;
  height: 100%;
  width: 100%;

  .cell-img {
    width: 100%;
    //max-height: 100px;
  }
}

.pagination-main {
  //background: #fff;
  /*padding-top: 5px;*/
  width: 100%;
  /*height: 35px;*/
  /*text-align: center;*/
  /*box-sizing: border-box;*/

  .pagination-class {
    display: inline-block;
  }

  .span-class {
    display: inline-block;
    font-size: 13px;
    text-align: center;
    margin-left: 15px;
  }
}

.input-wrap {
  top: 0px;
  left: 10px;
  width: 30px;

  .el-input__inner {
    height: 20px;
    padding: 0 5px;
  }
}

</style>
